package aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;
import java.util.Date;

//环绕通知
public class TimeAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object target=invocation.getThis();
        Method method=invocation.getMethod();
        Object[] args=invocation.getArguments();

        long start=0;
        long end=0;
        Object result=null;

        try {
            System.out.println("开始");
            start=System.currentTimeMillis();

            result=invocation.proceed();

            end=System.currentTimeMillis();
            System.out.println(method.getName()+" "+(end-start));
            System.out.println("结束");
        } catch (Throwable throwable) {
            end=System.currentTimeMillis();
            System.out.println(method.getName()+" "+(end-start));
            System.out.println("出错");
            throw throwable;
        }

        return result;
    }
}
